import common from '../vendor/common'
import '../scss/building_index.scss'
import buildingSelect from '../components/buildingSelect'
import { 
    getLikeBuildings,
    getRecommendsBuilding,
    postLookForHouse
  } from '../vendor/api'
const Vue = common.Vue
const buildingList = common.buildingList
var VueBuildingList = null
try{
    const mui = common.mui
    var mask = mui.createMask();
    // 下拉上拉插件初始化
    mui.init({
        swipeBack: false,
        pullRefresh: {
          container: '#pullrefresh',
          down: {
            style:'circle',
            callback: init_index
          },
          up: {
            height:50,//可选.默认50.触发上拉加载拖动距离
            auto:true,//可选,默认false.自动上拉加载一次
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'没有更多数据了',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback: pullupRefresh,
            contentinit: '',
            contentdown: ''
          }
        }
    });
    mui.plusReady(function(){
        VueBuildingList = new Vue({ // 猜你喜欢vue实例
            el: '#buildingList',
            data: {
                tel: '', // 用户输入手机号
                list: [], // 列表数据
                waiting: false, // 是否正在刷新页面
                page: 1, // 当前页数
                search: {} // 搜素条件
            },
            components: { buildingList, buildingSelect },
            methods: {
                changeData: function(data) {
                    var waitUI = plus.nativeUI.showWaiting("处理中，请等待...")
                    mask.show()
                    mui('#pullrefresh').pullRefresh().endPulldownToRefresh()
                    mui('#pullrefresh').pullRefresh().refresh(true)
                    this.waiting = false
                    this.list = []
                    var params = JSON.parse(JSON.stringify(data)) 
                    params.area_guid = data.area_guid === 'all'? null : data.area_guid
                    params.block_guid = data.block_guid === 'all'? null : data.block_guid
                    params.renovation = data.renovation === 'all'? null : data.renovation // 装修
                    params.features = data.features === 'all'? null : data.features // 决策偏好
                    this.search = params
                    getLikeBuildings(params).then(res => {
                        waitUI.close()
                        mask.close()
                        this.list = res.data.data
                        if (res.data.data.length<res.data.per_page) {
                            mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
                            document.querySelectorAll('div.mui-pull-caption.mui-pull-caption-nomore')[0].innerHTML = ''
                        }
                        this.page = parseInt(res.data.current_page) + 1
                        this.$nextTick(function(){
                            this.waiting = true
                        })
                    })
                    // 15s 后强制关闭 防止请求异常
                    setTimeout(function(){
                        waitUI.close()
                        mask.close()
                    }, 15000)
                },
                lookForHouse: function() {
                    var telRpx = /^\+?[1-9][0-9]*$/
                    if (!telRpx.test(this.tel) || this.tel.length !== 11) {
                        mui.toast('请输入正确的11位手机号')
                        return
                    }
                    postLookForHouse({ tel: this.tel, page_source: 'APP端楼盘列表' }).then(res => {
                        this.tel = ''
                        plus.nativeUI.toast('信息提交成功，楚楼网30分钟内联系您',{ duration:'long', type:'div' })
                    })
                }
            },
            mounted() {
                document.addEventListener('searchArea', function(event) {
                    VueBuildingList.$refs.buildingSelect.searchArea(event.detail.area_guid, event.detail.block_guid)
                })
            }
        })
        getLikeBuildings().then(res => {
            VueBuildingList.list = res.data.data
            VueBuildingList.page = parseInt(res.data.current_page) + 1
        })
    });
} catch(e){
    console.log('house_detail出错了')
}
// 初始化页面数据
function init_index() {
    VueBuildingList.$refs.buildingSelect.init()
    mui('#pullrefresh').pullRefresh().endPulldownToRefresh()
    mui('#pullrefresh').pullRefresh().refresh(true)
}
function pullupRefresh() {
    if (VueBuildingList.page === 1) {
        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false)
        return false
    }
    var params = JSON.parse(JSON.stringify(VueBuildingList.search))
    params.page = VueBuildingList.page
    getLikeBuildings(params).then(res => {
        mui('#pullrefresh').pullRefresh().endPullupToRefresh(false)
        if (res.data.data.length === 0) {
            mui('#pullrefresh').pullRefresh().endPullupToRefresh(true)
            return
        }
        for (var item of res.data.data) {
            VueBuildingList.list.push(item)
        }
        VueBuildingList.page = parseInt(res.data.current_page) + 1
    })
}